package com.wn.ticket.web;

import com.wn.ticket.common.PageBean;
import com.wn.ticket.common.ResponseEntity;
import com.wn.ticket.entity.Order;
import com.wn.ticket.entity.User;
import com.wn.ticket.service.OrderService;
import com.wn.ticket.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @创建人 NST
 * @创建时间 2022/8/29
 * @描述
 */
@Slf4j
@RestController
@RequestMapping("/front/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 增加订单，如果是现场取票方式，返回数据为订单编号
     *
     * @param order
     * @return
     */
    @RequestMapping("/add")//需提供user.id,deliveryMethod,receiver,receiveAddress,phoneNumber,entranceTicket.id,number七个参数
    public ResponseEntity getByCondition(@RequestBody Order order) {
        try {
            orderService.add(order);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("400", "fail", e.getMessage());
        }
        return new ResponseEntity("200","ok",order.getId());
    }

    @RequestMapping("list")
    public ResponseEntity getOrders(@RequestBody OrderVo orderVo) {
        PageBean<Order> bean = orderService.getByCondition(orderVo);
        return new ResponseEntity("200", "success", bean);
    }

    @RequestMapping("/mylist")
    public ResponseEntity getOrdersByUserId(@RequestBody User user) {
        List<Order> orders = orderService.getOrdersByUserId(user.getId());
        return new ResponseEntity("200", "success", orders);
    }

    @RequestMapping("/ticketlist")
    public ResponseEntity getTicketByUserId(@RequestBody User user) {

        return new ResponseEntity("200", "success", orderService.getTicketByUserId(user.getId()));
    }

    @RequestMapping("/update")
    public ResponseEntity updateState(@RequestBody Order order) {
        orderService.update(order);
        return ResponseEntity.success;
    }
}
